﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HaiHui.Business;
using HaiHui.Data;

namespace HaiHui
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class LoginForm : Window
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            string error = "";

            txtEmail.Text = txtEmail.Text.Trim();
            txtParola.Password = txtParola.Password.Trim();

            if (txtEmail.Text.Length == 0)
            {
                MessageBox.Show("Introduceti adresa de e-mail!", "Mesaj Haihui", MessageBoxButton.OK, MessageBoxImage.Warning);
                txtEmail.Focus();
                return;
            }
            else if (txtParola.Password.Length == 0)
            {
                MessageBox.Show("Introduceti parola!", "Mesaj Haihui", MessageBoxButton.OK, MessageBoxImage.Warning);
                txtParola.Focus();
                return;
            }

            bool loginResult = false;
            HttpUtils.Login(txtEmail.Text, txtParola.Password, out loginResult);
            if (!loginResult)
            {
                MessageBox.Show("Autentificare esuata!", "Mesaj Haihui", MessageBoxButton.OK, MessageBoxImage.Warning);
                txtEmail.Focus();
                return;
            }
            else
            {
                this.Visibility = Visibility.Hidden;

                DataStore.LoadAlbumUpdates();

                DataStore.ShowMainWindow += new EventHandler(DataStore_ShowMainWindow);


                
                
            }
        }

        void DataStore_ShowMainWindow(object sender, EventArgs e)
        {
            DataStore.ShowMainWindow -= new EventHandler(DataStore_ShowMainWindow);

            MainWindow win = new MainWindow();
            win.Closed += new EventHandler(win_Closed);
            win.ShowDialog();
            
        }

        void win_Closed(object sender, EventArgs e)
        {
            App.Current.Shutdown();
        }
    }
}
